window.onload = function () {
    showData(1);
}

function showData(pageNO) {
    axios.get("/room/findByItem", {
        params: {
            pageNO,
            address: $("addressText").value,
            state: $("stateSelect").value,
            info: $("infoSelect").value,
            type: $("typeSelect").value
        }
    }).then(resp => {
        let info = resp.data;
        let str = "";
        info.list.forEach(n => {
            str += `<tr><td>${n.address}</td><td>${n.maxNumber}</td><td>${n.number}</td><td>${n.state}</td><td>${n.type}</td>
<td><input type="button" value="删除" onclick="delRoom(${n.id})">
 <input type="button" value="查看房间"onclick="showRoom(${n.id})">
  <input type="button" value="查看设施"onclick="findFacility(${n.id})"></td></tr>`;
        });
        $("data").innerHTML = str;
        let cutStr = "";
        for (let i = 1; i <= info.toalPage; i++) {
            cutStr += `<a href="Javascript:showData(${i})" style="margin-left: 40px;text-decoration: none;color: black">${i}</a>`;
        }
        $("cutA").innerHTML = cutStr;
    })
}

/**
 *删除房间
 * @param id
 */
function delRoom(id) {
    axios.get("/room/del", {
        params: {
            id
        }
    }).then(resp => {
        let info = resp.data;
        if (info == "ok") {
            showData(1);
        } else {
            alert("删除失败房间内还有学生");
        }
    })
}

function showRoom(id) {

    axios.get("/room/findById", {
        params: {
            id
        }
    }).then(resp => {
        let info = resp.data;
        $("fRoomAddress").innerHTML = info.address;
        $("fRoomNum").innerHTML = info.maxNumber;
        $("fRoomState").innerHTML = info.state;
        $("fRoomStudentNum").innerHTML = info.number;
        $("fRoomType").innerHTML = info.type;

        let str = "";
        info.student.forEach(n => {
            str += `<tr><td>${n.name}</td><td>${n.classBean.name}</td><td>${n.phone}</td></tr>`;
        });
        $("studentData").innerHTML = str;
    })
    showDiv('findRoomDiv');
}

function findFacility(id) {
    $("roomIdText").value = id;
    showDiv('findFacilityDiv');
    axios.get("/facility/findByRoomId", {
        params: {
            roomId: id
        }

    }).then(resp => {
        let info = resp.data;
        let str = "";
        info.forEach(n => {
            str += `<tr><td>${n.msg}</td><td>${n.time}</td><td>${n.state}</td><td>`;
            if (n.state == "未解决") {
                str += `<input type="button" value="处理设施" onclick="updateStage(${n.id})">`;
            }
            str += `</td></tr>`;
        });
        $("facilityData").innerHTML = str;
    });
}

function updateStage(id) {

    axios.get("/facility/update", {
        params: {
            id,
            roomId: $("roomIdText").value
        }
    }).then(resp => {
        let info = resp.data;
        if (info == "ok") {
            findFacility($("roomIdText").value);
            showData(1);
        }
    })
}

function addState() {
    alert($("roomIdText").value + "添加");
    axios.get("/facility/add", {
        params: {
            msg: $("msgText").value,
            roomId: $("roomIdText").value
        }
    }).then(resp => {
        let info = resp.data;
        if (info == "ok") {
            findFacility($("roomIdText").value);
            showData(1);
        }
    });
}

function addRoom() {


    axios.get("/room/add", {
        params: {
            address: $("roomAddress").value,
            type: $("genderSelect").value,
            maxNumber: $("studentNum").value
        }
    }).then(resp => {
        let info = resp.data;
        if (info == "ok") {
            showData(1);
            closeDiv('addRoomDiv');
        }
    })
}

function showDiv(id) {
    $(id).style.visibility = "visible";
}

function closeDiv(id) {
    $(id).style.visibility = "hidden";
}